<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var maximalSquare = function (matrix) {
      let m = matrix.length, n = matrix[0].length,
        f = Array.from({ length: m + 1 }, () => Array(n + 1).fill(0)),
        max = 0
      for (let i = 1; i <= m; i++) {
        for (let j = 1; j <= n; j++) {
          if (matrix[i - 1][j - 1] === '1') {
            f[i][j] = Math.min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1
            max = Math.max(max, f[i][j])
          }
        }
      }
      return max ** 2
    };
    console.log(maximalSquare([["1", "0", "1", "0", "0"], ["1", "0", "1", "1", "1"], ["1", "1", "1", "1", "1"], ["1", "0", "0", "1", "0"]]))
  </script>
</body>

</html>